package com.lishe.lishe.lishe.base;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;
import com.lishe.lishe.R;
import com.lishe.lishe.lishe.common.AppManager;
import com.lishe.lishe.lishe.common.Globals;
import com.lishe.lishe.lishe.module.auth.activity.AuthActivity;
import com.lishe.lishe.lishe.utils.StatusBarUtil;
import com.umeng.analytics.MobclickAgent;


/**
 * Created by lwa on 2017/8/9.
 * activity基类
 */

public class BaseActivity extends AppCompatActivity {
    public Context context;
    private ProgressDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        setStatusBar();
        AppManager.getAppManager().addActivity(this);
//        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); //强制为横屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);       //强制为竖屏
    }

    protected void setStatusBar() {
        if (StatusBarUtil.setLightMode(this)) {
            StatusBarUtil.setColorNoTranslucent(this, getResources().getColor(R.color.white));
        } else {
            StatusBarUtil.setColor(this, getResources().getColor(R.color.white));
        }
    }

    @Override
    public Resources getResources() {
        //固定字体大小
        Resources res = super.getResources();
        Configuration config = new Configuration();
        config.setToDefaults();
        res.updateConfiguration(config, res.getDisplayMetrics());
        return res;
    }

    @Override
    protected void onPause() {
        super.onPause();
        //MobclickAgent.onPause(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        //MobclickAgent.onResume(this);
    }

    public void toast(String text) {
        if (!TextUtils.isEmpty(text)) {
            if (toast == null) {
                toast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
            }
            toast.setText(text);
            toast.show();
        }
    }

    Toast toast;

    public void toast(int resId) {
        if (toast == null) {
            toast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
        }
        toast.setText(getString(resId));
        toast.show();
    }

    /**
     * 显示Fragment
     *
     * @param container
     * @param target
     */
    public void jumpFragmentAdd(int container, Fragment target) {
        FragmentManager fm = getSupportFragmentManager();
        fm.beginTransaction().add(container, target).addToBackStack(null).commitAllowingStateLoss();
    }

    public void getDefaultProgressDialogs(String msg, boolean cancelable) {
        dialog = new ProgressDialog(context);
        dialog.setMessage(msg);
        dialog.setCancelable(cancelable);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                dialog = null;
            }
        });
        dialog.show();
    }

    public boolean dismissProgressDialogs() {
        if (dialog != null) {
            dialog.dismiss();
            dialog = null;
            return true;
        }
        return false;
    }

    /**
     * 判断是否已登录，否：跳转到 AuthActivity ，
     *
     * @return
     */
    protected void doCheckLoginIfNeedAndRedirectAuth(Activity act) {
        startActivity(new Intent(act, AuthActivity.class));
    }


    protected BaseApplication getMyApplication() {
        return (BaseApplication) (this.getApplication());
    }


    public Typeface getTypefaceRegular() {
        return getMyApplication().getTypefaceRegular();
    }

    public Typeface getTypeFaceMedium() {
        return getMyApplication().getTypeFaceMedium();
    }
}
